#include "total_flow.h"

total_flow::total_flow(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    ui_Label_Init();
    ui_Btn_Init();
   connect_Init();
}
void total_flow::connect_Init()
{

    //确定
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        keyPad *board = new keyPad(this);
         board->show();
        connect(board,&keyPad::changename2,this,&total_flow::Recivedensity);
        connect(board,&keyPad::destroyed,[=](){
            btnlist_ui.at(1)->setFocus();
        });

    });

    //返回
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });

}
void total_flow::ui_Label_Init()
{

    for(int i=0;i<14;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 20px;\
                                color: #123567;			                 /* 文本颜色 */\
                           }"
          );
    }
theLabel[0]->setGeometry(50,100,100,40);//显示气体名称
theLabel[1]->setGeometry(160,100,150,40);//显示本页面的功能
theLabel[2]->setGeometry(100,150,100,40);//最大值
theLabel[3]->setGeometry(220,150,100,40);//最大值的数据
theLabel[4]->setGeometry(350,150,100,40);//最大值的单位
theLabel[5]->setGeometry(100,195,100,40);//最小值
theLabel[6]->setGeometry(220,195,100,40);//最小值的数据
theLabel[7]->setGeometry(350,195,100,40);//最小值的单位
theLabel[8]->setGeometry(100,240,100,40);//设置值
theLabel[9]->setGeometry(220,240,100,40);//设置值的数据
theLabel[10]->setGeometry(350,240,100,40);//设置值的单位

theLabel[11]->setStyleSheet("QLabel{"
                      + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
theLabel[12]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
theLabel[13]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

theLabel[11]->setGeometry(0,0,640,60);//设定每隔放置一个按键
theLabel[12]->setGeometry(0,0,300,60);//设定每隔放置一个按键
theLabel[13]->setGeometry(340,0,300,60);//设定每隔放置一个按键
theLabel[11]->setText("总流量设置 ");

//目标浓度与钢瓶气浓度的比值，并确定总流量范围
float target_density = Sysset->Gas[GasSet_ABC-1].gasconcentration_span[GasSet_StandNum-1]/Sysset->Gas[GasSet_ABC-1].gasconcentration;
if(target_density != 0 && target_density <(Sysset->GasMFC_Range/(ZERO_set_data.toFloat()+Sysset->GasMFC_Range)))
{
     Min_totalflow = Stand_set_Min.toFloat()/target_density;                  //总流量最小值
     Max_totalflow = ZERO_set_data.toFloat()/(1-target_density);   //总流量最大值
     //显示可设置范围
     theLabel[6]->setText(QString::number(Min_totalflow,'f',0));
     theLabel[3]->setText(QString::number(Max_totalflow,'f',0));
}
else if(target_density >=(Sysset->GasMFC_Range/(ZERO_set_data.toFloat()+Sysset->GasMFC_Range)))
{
    Min_totalflow =ZERO_set_Min.toFloat()/(1-target_density) ;                  //总流量最小值
    Max_totalflow =Sysset->GasMFC_Range/target_density;   //总流量最大值
    //显示可设置范围
    theLabel[6]->setText(QString::number(Min_totalflow,'f',0));
    theLabel[3]->setText(QString::number(Max_totalflow,'f',0));
}
else
{
    Min_totalflow = 0;                  //此时目标浓度为0,总流量最小值即零气流量最小值
    Max_totalflow =0;   //总流量最大值
    theLabel[6]->setText("0");
    theLabel[3]->setText("0");
}
if(Sysset->Gas[GasSet_ABC-1].totalflow_span[GasSet_StandNum-1] >Max_totalflow ||
        Sysset->Gas[GasSet_ABC-1].totalflow_span[GasSet_StandNum-1] < Min_totalflow)
    {
      Sysset->Gas[GasSet_ABC-1].totalflow_span[GasSet_StandNum-1] = 0;
      theLabel[9]->setText("0");
    }
else
    theLabel[9]->setText(QString::number(Sysset->Gas[GasSet_ABC-1].totalflow_span[GasSet_StandNum-1],'f',3));








theLabel[0]->setText(tr(Sysset->Gas[GasSet_ABC-1].gasname));
theLabel[1]->setText(("标气")+QString::number(GasSet_StandNum)+("总流量："));
//确定可设置范围并显示
  StandMin = Sysset->Gas[GasSet_ABC-1].gasconcentration *1/(1+ZERO_set_data.toFloat());
  StandMax =Sysset->Gas[GasSet_ABC-1].gasconcentration * Stand_set_data.toFloat()/(Stand_set_data.toFloat()+100);
  QString temp = QString::number(StandMin,'f',3);
  QString temp2 = QString::number(StandMax,'f',3);
theLabel[2]->setText(tr("最大值："));
theLabel[3]->setText(temp);
theLabel[4]->setText("PPM");
theLabel[5]->setText(tr("最小值："));
theLabel[6]->setText(temp2);
theLabel[7]->setText("PPM");
theLabel[8]->setText(tr("设置为"));
theLabel[10]->setText("PPM");

if(Sysset->Gas[GasSet_ABC-1].gasconcentration_span[GasSet_StandNum-1] != 0)
    theLabel[9]->setText(QString::number(Sysset->Gas[GasSet_ABC-1].gasconcentration_span[GasSet_StandNum-1],'f',3));
else
    theLabel[9]->setText("0");
}

void total_flow::ui_Btn_Init()
{

    for(int i=0;i<2;i++)//创建主屏幕显示的按键
    {

         theBtn[i] = new QPushButton(this);
//        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
        theBtn[i]->geometry();
        theBtn[i]->setStyleSheet("QPushButton{\
                               outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                               background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                               border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                               border-style: outset;\
                               /* 按钮背景图标设置 */\
                               background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                               background-origin: content;\
                               background-position: top;	         /* 背景图片的位置 */\
                               padding-right: 0px;	    /*内边距设置*/	    \
                               padding-left: 0px;		/*  */	\
                               padding-bottom: 10px;		/*  */    \
                               padding-top: 10px;		/*  */    \
                               background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                               /* 按钮文本设置 */\
                               text-align: bottom;			             /* 文本的位置 */\
                               font-size: 20px;\
                               color: #FFFFFF;			                 /* 文本颜色 */\
                          }"
    "QPushButton:focus{\
outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
    background-color: rgb(255,125,25);	    /* 背景颜色 */\
    border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
    border-style: outset;\
    /* 按钮背景图标设置 */\
    background-image: url(:/pic/access/过滤.png); /* 背景图片 */\
    background-origin: content;\
    background-position: top;	         /* 背景图片的位置 */\
    padding-right: 0px;	    /*内边距设置*/	    \
    padding-left: 0px;		/*  */	\
    padding-bottom: 10px;	/*  */   \
    padding-top: 10px;		/*  */    \
    background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
    /* 按钮文本设置 */\
    text-align: bottom;			             /* 文本的位置 */\
    font-size: 22px;\
    qproperty-iconSize: 20px 20px;			/* 设置图标大小*/\
color: rgb(125, 255, 250);			                 /* 文本颜色 */\
}"
"QPushButton:pressed{background-color:rgb(99,211,133);}"
"QPushButton:checked{background-color:rgb(66,66,99);}"
    );
//        newbutton->setIconSize(QSize(1,2));

}
btnlist_ui.append(theBtn[0]);
btnlist_ui.append(theBtn[1]);

theBtn[0]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/切换.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/切换2.png); /* 背景图片 */"+"}");
theBtn[1]->setStyleSheet(  "QPushButton{"+the_StyleSheet[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
 "QPushButton:focus{"+the_StyleSheet[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

btnlist_ui.at(0)->setGeometry(150,300,120,90 );
btnlist_ui.at(1)->setGeometry(360,300,120,90 );
btnlist_ui.at(0)->setText(tr("确定"));
btnlist_ui.at(1)->setText(tr("返回"));
//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}


void total_flow::Recivedensity(QString s)
{
    //必须是在可设置的范围内设置才会有效
    float ss= s.toFloat();
    theLabel[9]->setText(QString::number(ss));
    if(ss>= Min_totalflow && ss<=Max_totalflow)
       Sysset->Gas[GasSet_ABC-1].totalflow_span[GasSet_StandNum-1]= ss;
    else if(ss< Min_totalflow )
    {
        theLabel[9]->setText(QString::number(Sysset->Gas[GasSet_ABC-1].totalflow_span[GasSet_StandNum-1],'f',3));
        the_Waring_Tip=("输入过低！");
        waring_Tip *menux = new waring_Tip(this);
        menux->show();
    }
    else if(ss >Max_totalflow)
    {
        theLabel[9]->setText(QString::number(Sysset->Gas[GasSet_ABC-1].totalflow_span[GasSet_StandNum-1],'f',3));
        the_Waring_Tip=("输入过高！");
        waring_Tip *menux = new waring_Tip(this);
        menux->show();
    }
}

bool total_flow::eventFilter(QObject *watched, QEvent *event)
{
    if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched)
{
    if(event->type() == QEvent::KeyPress)
    {
        QKeyEvent *ev = (QKeyEvent *)event;
        QPushButton *button = (QPushButton *)watched;

       {
           switch (ev->key())
           {
           case OK:
           {
               emit  button-> clicked();
             break;
           }
           case UP: //Up
             {

               focusPreviousChild();
               break;
             }
           case DOWN: //Down
             {
              focusNextChild();
               break;
             }
           case BACK:
           {
               this->parentWidget()->show();
               this->close();
               break;
           }
           case INIT:
           {
               init_Show *show = new init_Show(this);
               show->show();
               this->hide();

               break;
           }
            default:
               break;
           }
       }
    }
    }
    return QWidget::eventFilter(watched,event);//返回父类的事件过滤器

}
